package map;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;

/**
 * An item entry for use with the MapLibrary class.
 * Each entry has a unique identification number, a name,
 * and an image.
 * Items also have a width and a height because
 * they may occupy more than 1 square.
 * @author Alex Babis
 */
public class MapLibraryItemEntry
{
	private int id;
	private String name;
	private Image image;
	private Image image90CW;
	private Image image180CW;
	private Image image270CW;
	
	private int width;
	private int height;
	
	/**
	 * Builds a new item entry for a MapLibrary
	 * @param id		The unique ID for this entry
	 * @param name		The name of this entry
	 * @param image		The image associated with this entry
	 */
	public MapLibraryItemEntry(int id, String name, Image image)
	{
		this.id = id;
		this.name = name;
		this.image = image;
		image90CW = getRotatedImage(image);
		image180CW = getRotatedImage(image90CW);
		image270CW = getRotatedImage(image180CW);
		width = 1;
		height = 1;
	}
	
	/**
	 * Builds a new item entry for a MapLibrary with a specified size
	 * @param id		The unique ID for this entry
	 * @param name		The name of this entry
	 * @param image		The image associated with this entry
	 * @param width		The width in squares of this this entry
	 * @param height	The height in squares of this entry
	 */
	public MapLibraryItemEntry(int id, String name, Image image, int width, int height)
	{
		this.id = id;
		this.name = name;
		this.image = image;
		image90CW = getRotatedImage(image);
		image180CW = getRotatedImage(image90CW);
		image270CW = getRotatedImage(image180CW);
		this.width = width;
		this.height = height;
	}

	// Thanks to:
	// http://stackoverflow.com/questions/706665/lossless-jpeg-rotate-90-180-270-degrees-in-java
	/**
	 * Returns an Image that is a rotated copy of given image
	 * @param image The Image to be rotated
	 * @return A rotated copy of given image
	 */
	private Image getRotatedImage(Image image)
	{
		Image rotatedImage = new BufferedImage(image.getHeight(null),
				image.getWidth(null), BufferedImage.TYPE_INT_ARGB);

	    Graphics2D g2d = (Graphics2D) rotatedImage.getGraphics();
	    g2d.rotate(Math.toRadians(90.0));
	    g2d.drawImage(image, 0, -rotatedImage.getWidth(null), null);
	    g2d.dispose();
	    return rotatedImage;
	}
	
	/** Compares this entry to another
	 * @return True if the id of this entry equals that of the other; false otherwise */
	public boolean equals(Object other){return ((MapLibraryItemEntry)other).id == id;}
	
	/** Compares this entry to another
	 * @return An integer comparing these two entries */
	public int compareTo(MapLibraryItemEntry other){return id - other.id;}
	
	/** Gets the unique ID for this terrain entry
	 * @return The unique ID for this terrain entry */
	public int getID(){return id;}
	
	/** Gets the name of this terrain entry 
	 * @return The name of this terrain entry */
	public String getName(){return name;}
	
	/** Gets the Image for this terrain entry
	 * @return The Image for this terrain entry */
	public Image getImage(){return image;}
	
	/** Gets the Image for this terrain entry
	 * @return The Image for this terrain entry */
	public Image getImage(int rotation)
	{
		switch(rotation)
		{
			case 0:
				return image;
			case 1:
				return image90CW;
			case 2:
				return image180CW;
			case 3:
				return image270CW;
		}
		return null;
	}

	/** Gets the width in squares of this terrain entry
	 * @return The width in squares of this terrain entry */
	public int getWidth(){return width;}
	
	/** Gets the height in squares of this terrain entry
	 * @return The height in squares of this terrain entry */
	public int getHeight(){return height;}
	
	/** Returns a String representation for this entry
	 * @return A String representation of this entry */
	public String toString(){return id + " " + name;}
}